//
//  JobInformationViewController.m
//  TenThousand
//
//  Created by 我的 on 16/7/7.
//  Copyright © 2016年 baixue. All rights reserved.
//

#import "JobInformationViewController.h"
#import "PersonTableViewCell.h"
/**
 * 工作信息
 */

@interface JobInformationViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
{
    NSString *_getText;
    NSMutableDictionary *_jobDic;
}

@property (nonatomic, strong) NSMutableArray *userInfoClassArrM;

@property (nonatomic, assign) NSInteger workInfo;
@property (nonatomic, assign) NSInteger enterprise;
@property (nonatomic, assign) NSInteger industry;

@end
#define PERSONALARR @[@"公司名称",@"营业执照号",@"企业性质",@"行业分类",@"工作性质",@"税前月收入",@"工作电话",@"公司地址"]
#define INFORMATION @[@"请填写公司名称",@"请填写营业执照号",@"请选择",@"请选择",@"请选择",@"请填写税前收入",@"请填写公司号码",@"请填写公司地址"]
#define WorkInfo @[@"Compay",@"Business",@"Enterprise",@"Industry",@"Working",@"Pretax",@"WorkPhone",@"Address"]

@implementation JobInformationViewController

- (NSMutableDictionary *)userInfoDicM {
    if (!_userInfoDicM) {
        _userInfoDicM = [NSMutableDictionary dictionary];
    }
    return _userInfoDicM;
}

- (NSMutableArray *)userInfoClassArrM {
    if (!_userInfoClassArrM) {
        _userInfoClassArrM = [NSMutableArray array];
    }
    return _userInfoClassArrM;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createLeft:[UIImage imageNamed:@"ZHSZicon_03"] leftClick:^{
        [self.navigationController popViewControllerAnimated:YES];
    } right:nil rightClick:nil];
    
    [self.view addSubview:self.jobTableView];
    self.navigationItem.title = @"设置工作信息";
    _jobDic = [NSMutableDictionary dictionary];
    
      self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonClick:)];
}

- (void)getUserInfoClassDate:(NSString *)typeId {
    /*类型 TypeId
     0.工作情况 1.教育情况  2.企业性质  3.行业分类 4.住房情况*/
    NSDictionary *dict1 = @{
                            @"TypeId":typeId
                            };
    //用户基础信息资料读取接口
    [NetWorkManager requestWithType:HttpRequestTypePost withMethodName:@"User.GetUserInfoClass" withParaments:dict1 withSuccessBlock:^(NSDictionary *object) {
        NSLog(@"%@", object);
        [self.userInfoClassArrM removeAllObjects];
        [self.userInfoClassArrM addObjectsFromArray:object[@"Data"]];
        [self showPickerView:typeId];
    } withFailureBlock:^(NSError *error) {
        
        [ToastView showErrorWithStatus:@"网络请求失败"];
    } progress:^(float progress) {
        
    }];
    
}

- (void)showPickerView:(NSString *)typeId {
    NSMutableArray *arrM = [NSMutableArray array];
    for (NSDictionary *dic in self.userInfoClassArrM) {
        [arrM addObject:dic[@"InfoName"]];
    }
    NSString *key = @"";
    NSInteger indexPath;
    if ([typeId integerValue] == 2) {
        key = @"enterpriseNature";
        indexPath = 2;
    } else if ([typeId integerValue] == 3) {
        key = @"classification";
        indexPath = 3;
    } else {
        key = @"workInfo";
        indexPath = 4;
    }
    
    BXPickerView *pickerView = [[BXPickerView alloc] initWithFrame:CGRectMake(0, Screen_Height-220, Screen_Width, 220) WithTitleListArr:arrM WithIndex:1];
    
    //取值
    pickerView.clickedBtnIndexBlock = ^(NSString *text, NSInteger index){
        _getText = text;
        if ([typeId integerValue] == 2) {
            self.enterprise = index + 1;
            [_jobDic setValue:[NSString stringWithFormat:@"%ld", self.enterprise] forKey:[key stringByAppendingString:@"Num"]];
        } else if ([typeId integerValue] == 3) {
            if (index == 0) {
                self.industry = index + 1;
            } else if (index == 1) {
                self.industry = index + 9;
            } else if (index == 2) {
                self.industry = index + 6;
            } else {
                self.industry = index + 8;
            }
            [_jobDic setValue:[NSString stringWithFormat:@"%ld", self.industry] forKey:[key stringByAppendingString:@"Num"]];
            [_jobDic setValue:text forKey:key];
        } else {
            if (index == 0) {
                self.workInfo = index + 1;
            } else {
                self.workInfo = index + 4;
            }
            
            [_jobDic setValue:[NSString stringWithFormat:@"%ld", self.workInfo] forKey:[key stringByAppendingString:@"Num"]];
        }
        [_jobDic setValue:_getText forKey:key];
        NSIndexSet * nd=[[NSIndexSet alloc]initWithIndex:indexPath];
        [_jobTableView reloadSections:nd withRowAnimation:UITableViewRowAnimationAutomatic];
    };
    [pickerView showView];
}


- (void)rightButtonClick:(UIBarButtonItem *)item{
    
    if (self.ReturnJobDicBlock != nil) {
        self.ReturnJobDicBlock(_jobDic);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if (self.ReturnJobDicBlock != nil) {
        self.ReturnJobDicBlock(_jobDic);
    }
}

- (UITableView *)jobTableView{
    if (!_jobTableView) {
        _jobTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height-64) style:UITableViewStyleGrouped];
        _jobTableView.delegate = self;
        _jobTableView.dataSource = self;
        _jobTableView.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1.00];
        _jobTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.jobTableView.showsVerticalScrollIndicator = NO;
        _jobTableView.scrollEnabled = NO;
    }
    return _jobTableView;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 8;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    PersonTableViewCell *personalCell = [tableView dequeueReusableCellWithIdentifier:@"PERSONALTABLEVIEWCELL"];
    if (personalCell == nil) {
        personalCell = [[[NSBundle mainBundle] loadNibNamed:@"PersonTableViewCell" owner:nil options:nil] lastObject];
    }
    
    personalCell.selectionStyle = UITableViewCellSelectionStyleNone;
    personalCell.informationLable.text = PERSONALARR[indexPath.section];

    //设置向右的箭头
    if (indexPath.section > 1 && indexPath.section < 5) {
        personalCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        personalCell.inforTextfield.enabled = NO;
    }
    if ([[self.userInfoDicM objectForKey:WorkInfo[indexPath.section]] isEqualToString:@""]) {
        personalCell.inforTextfield.placeholder = INFORMATION[indexPath.section];
    } else {
        personalCell.inforTextfield.placeholder = [self.userInfoDicM objectForKey:WorkInfo[indexPath.section]];
    }

    personalCell.inforTextfield.tag = indexPath.section + 1000;
    personalCell.inforTextfield.delegate = self;
    
    personalCell.inforTextfield.text = _getText;
    
    return personalCell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}


-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section == 0 ) {
        return 15;
    }else if(section == 2 || section == 6){
        return 20;
    }else{
        return 8;
    }
    return 0.01;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    switch (indexPath.section) {
        case 2:
        {
            [self getUserInfoClassDate:@"2"];
//            NSMutableArray *arr = (NSMutableArray *)@[@"外资企业",@"国有企业",@"私营企业",@"合资企业"];
//            BXPickerView *pickerView = [[BXPickerView alloc] initWithFrame:CGRectMake(0, Screen_Height-220, Screen_Width, 220) WithTitleListArr:arr WithIndex:1];
//            
//            //取值
//            pickerView.clickedBtnIndexBlock = ^(NSString *text){
//                _getText = text;
//                [_jobDic setValue:_getText forKey:@"enterpriseNature"];//企业性质
//                NSIndexSet * nd=[[NSIndexSet alloc]initWithIndex:2];//刷新第二个section
//                [_jobTableView reloadSections:nd withRowAnimation:UITableViewRowAnimationAutomatic];
//            };
//            [pickerView showView];
        }
            break;
        case 3:
            
        {
            [self getUserInfoClassDate:@"3"];
//            NSMutableArray *arr = (NSMutableArray *)@[@"生产|物流|质控|汽车",@"法律|教育|翻译|出版",@"销售|客服|采购|淘宝",@"市场|媒介|广告|设计",@"人力|行政|管理",@"网络|通信|电子",@"建筑|物业|其他",@"财会|金融|保险",@"医疗|制药|环保",@"生活|服务"];
//            BXPickerView *pickerView = [[BXPickerView alloc] initWithFrame:CGRectMake(0, Screen_Height-220, Screen_Width, 220) WithTitleListArr:arr WithIndex:1];
//            
//            //取值
//            pickerView.clickedBtnIndexBlock = ^(NSString *text){
//                _getText = text;
//                [_jobDic setValue:_getText forKey:@"classification"];
//                NSIndexSet * nd=[[NSIndexSet alloc]initWithIndex:3];//刷新第二个section
//                [_jobTableView reloadSections:nd withRowAnimation:UITableViewRowAnimationAutomatic];
//            };
//            [pickerView showView];
        }
            break;
        case 4:
        {
            [self getUserInfoClassDate:@"0"];
//            NSMutableArray *arr = (NSMutableArray *)@[@"互联网/电子商务",@"IT服务/系统集成",@"计算机硬件",@"计算机软件",@"通信/电信",@"电子技术/半导体/集成电路",@"仪器仪表/工业自动化",@"贸易/进出口",@"财务/审计",@"金融/银行",@"保险",@"批发/零售",@"快速消费品(食品/饮料等)",@"耐用消费品(家具/家电等)",@"服装/纺织/皮革",@"办公室用品及设备",@"钢铁/机械/设备/重工",@"汽车/摩托车",@"医疗/保健/卫生/美容",@"生物/制药/医疗机械",@"广告/创意",@"公关/市场推广/会展",@"文体/影视/艺术",@"媒体传播",@"出版/印刷/造纸",@"房地产/物业管理",@"建筑/建材",@"家居/室内设计/装潢",@"中介/专业服务",@"检测/认证",@"法律/法务",@"教育/科研/培训",@"旅游/酒店",@"娱乐休闲/餐饮/服务",@"交通/运输/物流",@"航天/航空",@"化工/采掘/冶炼",@"能源(电力/水利/矿产)",@"原材料和加工",@"政府/非盈利机构",@"环保",@"农林牧渔",@"多元化集团",@"人力资源服务",@"其他"];
//            BXPickerView *pickerView = [[BXPickerView alloc] initWithFrame:CGRectMake(0, Screen_Height-220, Screen_Width, 220) WithTitleListArr:arr WithIndex:1];
//            
//            //取值
//            pickerView.clickedBtnIndexBlock = ^(NSString *text){
//                _getText = text;
//                [_jobDic setValue:_getText forKey:@"workInfo"];//工作性质
//                NSIndexSet * nd=[[NSIndexSet alloc]initWithIndex:4];//刷新第二个section
//                [_jobTableView reloadSections:nd withRowAnimation:UITableViewRowAnimationAutomatic];
//            };
//            [pickerView showView];
        }
            break;
            
        default:
            break;
    }
    
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    
//    if (textField.tag == 1000) {
//        [_jobDic setValue:textField.text forKey:@"companyName"];
//    }else if (textField.tag == 1001){
//        [_jobDic setValue:textField.text forKey:@"license"];
//    }else if (textField.tag == 1005){
//        [_jobDic setValue:textField.text forKey:@"income"];
//    }
//    else if (textField.tag == 1006){
//        [_jobDic setValue:textField.text forKey:@"workPhone"];
//    }else if (textField.tag == 1007){
//        [_jobDic setValue:textField.text forKey:@"companyAddress"];
//
//    }
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    if (textField.tag == 1000) {
        [_jobDic setValue:[textField.text stringByAppendingString:string] forKey:@"companyName"];
    }else if (textField.tag == 1001){
        [_jobDic setValue:[textField.text stringByAppendingString:string] forKey:@"license"];
    }else if (textField.tag == 1005){
        [_jobDic setValue:[textField.text stringByAppendingString:string] forKey:@"income"];
    }
    else if (textField.tag == 1006){
        [_jobDic setValue:[textField.text stringByAppendingString:string] forKey:@"workPhone"];
    }else if (textField.tag == 1007){
        [_jobDic setValue:[textField.text stringByAppendingString:string] forKey:@"companyAddress"];
        
    }
    return YES;
    
}

@end
